package com.quark.framework.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.quark.framework.configuration.QuarkConfigManager;
import com.quark.framework.utility.QuarkLog;

public class QuarkServiceManager {

	private QuarkService[] serviceList = null;
	private static QuarkServiceManager inst = new QuarkServiceManager();
	public QuarkServiceManager()
	{
		
	}
	public static QuarkServiceManager getInstance()
	{
		return inst;	
	}
	public boolean init()
	{
		QuarkServiceConfiguration serviceConfig = (QuarkServiceConfiguration)QuarkConfigManager.getInstance().getConfiguration(QuarkServiceConfiguration.class);
		serviceList = new QuarkService[serviceConfig.serviceModuleList.length];
		boolean ret = true;
		for(int i = 0; i < serviceList.length; i++)
		{
			try {
				serviceList[i] = QuarkService.class.cast(Class.forName(serviceConfig.serviceModuleList[i]));
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				QuarkLog.log(e);
				ret = false;
			}
		}
		return ret;
	}
	
	public Object dispatchRequest(Map<String, String[]> request)
	{
		String[] funcName = request.get("req");
		if(funcName == null)
			return null;
		request.remove("req");
		for(int i = 0; i < serviceList.length; i++)
		{
			if(serviceList[i].hasFunction(funcName[0], request.size()))
			{
				Object ret = serviceList[i].work(funcName[0], request);
				return ret;
			}
		}
		QuarkLog.logDebug("Unknow Request:" +  funcName[0]);
		return null;
	}
	
}
